package com.aosai.localtax.activity;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.aosai.localtax.R;
import com.aosai.localtax.app.BaseActivity;
import com.aosai.localtax.model.MainViewPagerModel;
import com.squareup.okhttp.Response;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import pw.hais.http.Http;
import pw.hais.http.base.OnHttpListener;
import pw.hais.utils.EmptyUtil;

/**
 * Created by hais1992 on 2016/1/16/016.
 */
public class ArticleDetailsActivity extends BaseActivity {
    private WebView webview;
    private MainViewPagerModel article;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        article = (MainViewPagerModel) getIntent().getSerializableExtra("Article");

        webview = (WebView) findViewById(R.id.webview);

        getSupportActionBar().setTitle(article.title + "");

        WebSettings settings = webview.getSettings();
        settings.setSupportZoom(true);          //支持缩放
        settings.setBuiltInZoomControls(true);  //启用内置缩放装置
        settings.setJavaScriptEnabled(true);    //启用JS脚本
        settings.setUseWideViewPort(true);//关键点
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

        webview.setWebViewClient(new WebViewClient() {
            //当点击链接时,希望覆盖而不是打开新窗口
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);  //加载新的url
                return true;    //返回true,代表事件已处理,事件流到此终止
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });


        Http.get(article.url, null, new OnHttpListener<String>() {
            @Override
            public void onSuccess(Response response, String data) {
                Document document = Jsoup.parse(data);
                if(EmptyUtil.emptyOfString(document.getElementsByClass("news-title").text()))return;
                StringBuffer sb = new StringBuffer();
                String html = document.getElementsByClass("news-contents").html();
                sb.append("\n" +
                        "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">  \n" +
                        "<html>  \n" +
                        "    <head>  \n" +
                        "        <meta charset=\"utf-8\" />  \n" +
                        "        <title>scroll</title>  \n" +
                        "        <style type=\"text/css\">\n" +
                        "            body\n" +
                        "            {\n" +
                        "                margin:0px;\n" +
                        "                padding:0px;\n" +
                        "                overflow:hidden;\n" +
                        "            }\n" +
                        "            img\n" +
                        "            {\n" +
                        "                width:100%;\n" +
                        "                margin:0 auto 10px;\n" +
                        "            }\n" +
                        "            .container {\n" +
                        "                width : 100%;\n" +
                        "                height : 100%;\n" +
                        "                -webkit-box-sizing : border-box;\n" +
                        "                position : relative;\n" +
                        "                overflow-y : auto;\n" +
                        "                overflow-x : hidden;\n" +
                        "                -webkit-overflow-scrolling : touch;  /* liuhx:可以把这整行注释掉对比差别 */\n" +
                        "                padding:10px;\n" +
                        "            }\n" +
                        "\n" +
                        "        </style>\n" +

                        "      <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n" +
                        "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
                        "<meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0\" />\n" +
                        "<meta name=\"apple-mobile-web-app-capable\" content=\"yes\">\n" +
                        "<meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\">\n" +
                        "<meta name=\"format-detection\" content=\"telephone=no\">" +


                        "<body>\n" +
                        "    <div  id=\"main\" align=\"center\" class=\"container\">");


                //标题
                sb.append("<h3>" + document.getElementsByClass("news-title").text() + "</h3>");
                //时间
                sb.append(document.getElementsByClass("news-detail").select("span").last().text() + "</br></br>");
                //图片
                Elements imgelements = document.getElementById("contents").select("img");
                for (Element e : imgelements) {
                    String image_url = e.attr("src");
                    sb.append("<img src = \"http://www.gxds.gov.cn" + image_url + "\"/>");
                }
                sb.append(document.getElementById("contents").html());

                sb.append("   </div>\n" +
                        "</body>  \n" +
                        "</html> ");

                html = sb.toString();


                webview.loadData(html, "text/html; charset=UTF-8", null);
            }
        });




    }
}
